import Vue from 'vue'
import Router from 'vue-router'
import routes from './routers'
import store from '@/store/index'

Vue.use(Router)
const router = new Router({
  routes,
  mode: 'history'
})
const HOME_NAME = 'login'

router.beforeEach((to, from, next) => {
  document.title = to.meta.title
  const token = store.state.token
  if (to.name === HOME_NAME) {
    next()
  } else if (to.name === 'result') {
    next()
  } else {
    if (token) {
      next()
    } else {
      console.log('该页面需要登录')
      next({
        path: '/login'
        // query: {redirect: to.fullPath} // 将跳转的路由path作为参数，登录成功后跳转到该路由
      })
    }
  }
})

export default router
